package com.goro;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.model.SelectItem;

import com.joro.Magazine;
import com.joro.Supplier;
import com.joro.ejb.SupplierFacadeLocal;

public class SupplierBean {

	@EJB
	protected SupplierFacadeLocal supplierDao;
	public Supplier supplier;
	
	
	public Supplier getSupplier() {
		return supplier;
	}

	public void setSupplier(Supplier supplier) {
		this.supplier = supplier;
	}

	public SupplierBean() {
		
	}
	
	public String deleteSupplier(){
		supplierDao.deleteSupplier(this.supplier);
		return "successful_delete";
	}

	public Collection<Supplier> getSuppliers() {
		return supplierDao.getSuppliers();
	}
	
	public Collection<String> getSuppliersAsString() {
		List<String> res = new ArrayList<String>();
		Collection<Supplier> suppliers = supplierDao.getSuppliers();
		for (Supplier supplier : suppliers) {
			res.add(supplier.getName());
		}
		return res;
	}
	
	public Collection<SelectItem> getSupplierItems() {
		List<SelectItem> res = new ArrayList<SelectItem>();
		Collection<Supplier> suppliers = supplierDao.getSuppliers();
		for (Supplier supplier : suppliers) {
			SelectItem item = new SelectItem();
			item.setLabel(supplier.getName());
			item.setValue(supplier.getId());
			res.add(item);
		}
		return res;
	}
}
